package com.csun.cmny.provider.config;

import com.csun.cmny.provider.device.sleepcare.entity.SleepCareConstant;
import com.csun.cmny.provider.task.AppSleepDataQuartz;
import com.csun.cmny.provider.task.QuartzTask;
import com.csun.cmny.provider.task.RefreshExpireKeyQuartz;
import com.csun.cmny.provider.task.TestQuartz;
import com.csun.cmny.provider.utils.QuartzManagerUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-22 19:39
 * @Description: the class quartz config
 */
@Slf4j
@Configuration
public class CmnyQuartzConfig {

//    @Resource
//    private QuartzManagerUtil quartzManager;

    @Bean
    public JobDetail sleepCareQuartzDetail() {
        return JobBuilder.newJob(QuartzTask.class).withIdentity("sleepCareQuartz").storeDurably().build();
    }

    @Bean
    public Trigger sleepCareQuartzTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(SleepCareConstant.SLEEP_CARE_FREQUENCY)  // 设置时间周期单位秒
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(sleepCareQuartzDetail())
                .withIdentity("sleepCareQuartz")
                .withSchedule(scheduleBuilder)
                .build();
    }

//    @Bean
//    public JobDetail testQuartzDetail() {
//        return JobBuilder.newJob(TestQuartz.class).withIdentity("testQuartz").storeDurably().build();
//    }
//
//    @Bean
//    public Trigger testQuartzTrigger() {
//        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
//                .withIntervalInSeconds(1)  // 设置时间周期单位秒
//                .repeatForever();
//        return TriggerBuilder.newTrigger().forJob(testQuartzDetail())
//                .withIdentity("testQuartz")
//                .withSchedule(scheduleBuilder)
//                .build();
//    }

    @Bean
    public JobDetail secondRefreshQuartzDetail() {
        return JobBuilder.newJob(RefreshExpireKeyQuartz.class).withIdentity("secondRefresh").storeDurably().build();
    }

    @Bean
    public Trigger secondRefreshQuartzTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1)  // 设置时间周期单位秒
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(secondRefreshQuartzDetail())
                .withIdentity("secondRefresh")
                .withSchedule(scheduleBuilder)
                .build();
    }


//    @Bean
//    public AppSleepDataQuartz saveAppSleepData() {
//        log.info(">>>>>>>>>>>>>>>保存睡眠数据定时任务开启<<<<<<<<<<<<<<<<<<<");
//        String JOB_GROUP_NAME = "JOB_GROUP_NAME_APP_SLEEP_DATA";
//        String TRIGGER_GROUP_NAME = "TRIGGER_GROUP_NAME_PERIOD_APP_SLEEP_DATA";
//        //每天中午12:01生成
//        String cron = "0 01 12 * * ?";
//        Map<String, Object> map = new JobDataMap();
//        try {
//            quartzManager.addJob("AppSleepDataQuartz", JOB_GROUP_NAME, "AppSleepDataQuartzTrigger", TRIGGER_GROUP_NAME, AppSleepDataQuartz.class, cron, map);
//
//        } catch (Exception e) {
//            log.info(">>>>>>>>>>>>>>>保存睡眠数据定时任务失败<<<<<<<<<<<<<<<<<<<");
//        }
//        return new AppSleepDataQuartz();
//    }
}
